/**
 * 
 */
package oauth.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import oauth.repository.mapper.SysFingerMapper;
import oauth.repository.mapperadd.SpringSecurityMapper;
import oauth.repository.mapperadd.SysUsersMapperAdd;
import oauth.repository.model.SysFinger;
import oauth.service.finger.FingerMatchWS;
import oauth.service.security.SpringSecurityLogin;

/**
 * @author Pan JinWen
 * @time 2016年6月5日下午5:25:34
 */
@Controller
public class Security
{
	@Autowired
	private SpringSecurityMapper springSecurityMapper;
	@Autowired
	private SysFingerMapper sysFingerMapper;
	@Autowired
	private SysUsersMapperAdd sysUsersMapperAdd;
	@Autowired
	private SpringSecurityLogin springSecurityLogin;
	@Autowired
	private FingerMatchWS fingerMatchWS;

	@RequestMapping("/username")
	public @ResponseBody String username()
	{
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String name = authentication.getName();
		BigDecimal selectUserIdByUsername = sysUsersMapperAdd.selectUserIdByUsername(name);
		return new String(name + ":" + selectUserIdByUsername);
	}

	@RequestMapping(value = "/finger", method = RequestMethod.GET)
	public String Show()
	{
		return "fingerlist";
	}

	// 更新用户指纹
	@RequestMapping(value = "/finger", method = RequestMethod.PUT)
	public @ResponseBody Map<String, Object> update(@RequestBody SysFinger sysFinger)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		if (sysFinger.getUserId() == null)
		{
			map.put("alert alert-error", "无法检测当前用户！");
			return map;
		}
		if (springSecurityMapper.updateSysFingerByUserIdAndFingerId(sysFinger) == 0)
		{
			sysFingerMapper.insertSelective(sysFinger);
			map.put("alert alert-success", "新增指纹成功！");
		}
		else
		{
			map.put("alert alert-info", "指纹更新成功！");
		}

		return map;
	}

	// 查询用户指纹
	@RequestMapping(value = "/fingers/{id}", method = RequestMethod.POST)
	public @ResponseBody List<SysFinger> list(@PathVariable Integer id, Map<String, Object> map)
	{
		BigDecimal selectUserIdByUsername = new BigDecimal(id);
		List<SysFinger> sysFingers = new ArrayList<SysFinger>();
		if (selectUserIdByUsername == null) { return sysFingers; }
		sysFingers = springSecurityMapper.selectSysFingerByUserId(selectUserIdByUsername);
		if (sysFingers.size() == 0)
		{
			sysFingers.add(new SysFinger());
			sysFingers.get(0).setUserId(selectUserIdByUsername);
		}
		return sysFingers;
	}

	// 自定义授权
	@SuppressWarnings("unused")
	@RequestMapping(value = "/fingerlogin", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> login(@RequestBody Map<String, Object> map, HttpServletRequest request)
	{
		String fingercode = null;
		String username = null;
		if (map.containsKey("fingercode"))
		{
			fingercode = (String) map.get("fingercode");
		}
		else
		{
			map.put("error", "请输入指纹特征码！");
			return map;
		}
		// 根据fingercode进行全库查询，并返回username和finger_id
		if (fingercode == null || fingercode.trim().equals(""))
		{
			map.put("error", "指纹特征码获取错误！");
			return map;
		}
		else
		{
			// 匹配指纹
			map = fingerMatchWS.match(fingercode);
			System.err.println("返回 " + map.size() + " 信息！");
			if (map.containsKey("error"))
			{
				return map;
			}
			else
			{
				// 获取用户名
				username = (String) map.get("username");
				if (springSecurityLogin.login(username, request) == 1)
				{
					map.put("success", " 登录成功！ 用户为：" + map.get("username") + "，手指为：" + map.get("finger_id") +
							"根，指纹对比得分为：" + map.get("score"));
				}
				else
				{
					map.put("error", "认证过程错误，登录失败！");
				}
				return map;
			}
		}
	}

	// 指纹比对对外接口
	@RequestMapping(value = "/fingerMatch", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> fingerMatch(@RequestBody String fingercode)
	{
		return fingerMatchWS.match(fingercode);
	}

}
